package cn.com.qc.main;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.qc.R;
import cn.com.qc.adapter.JobAdapter;
import cn.com.qc.help.NetUrl;
import cn.com.qc.javabean.JobInfo;
import cn.com.qc.utils.Tools;
import cn.com.qc.view.ExpandTabView;
import cn.com.qc.view.SwipeToLoadLayout;
import cn.com.qc.view.ViewLeft;
import cn.com.qc.view.ViewMiddle;
import cn.com.qc.view.ViewRight;
import cn.com.qc.yinter.OnLoadMoreListener;
import cn.com.qc.yinter.OnRefreshListener;

public class SearchByproActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener, TextView.OnEditorActionListener {
    @BindView(R.id.swipe_target)
    ListView swipeTarget;
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    private List<JobInfo> jobInfoList;
    private JobAdapter jobAdapter;
    private int pageNumber = 1;
    private Integer total_pages = 0;
    private String searchBody;
    private String WorkPlace;
    private ExpandTabView expandTabView;
    private ArrayList<View> mViewArray = new ArrayList<View>();
    private ViewLeft viewLeft;
    private ViewMiddle viewMiddle;
    private ViewRight viewRight;
    private String MinMoney = "";
    private String Education = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchbypro);
        Tools.initSystemBarTint(this);
        ButterKnife.bind(this);
        searchEdit.setOnEditorActionListener(this);
        expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view);
        viewLeft = new ViewLeft(this);
        viewMiddle = new ViewMiddle(this);
        viewRight = new ViewRight(this);
        jobInfoList = new ArrayList<>();
        initVaule();
        initData();
        GetSearch_job(pageNumber);
        jobAdapter = new JobAdapter(jobInfoList, this);
        swipeTarget.setAdapter(jobAdapter);
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        initListener();
    }

    private void initData() {
        searchBody = getIntent().getStringExtra("searchBody");
        searchEdit.setText(searchBody);
    }
    private void initVaule() {
        mViewArray.add(viewMiddle);
        mViewArray.add(viewLeft);
        mViewArray.add(viewRight);
        ArrayList<String> mTextArray = new ArrayList<String>();
        mTextArray.add("城市");
        mTextArray.add("薪资");
        mTextArray.add("教育水平");

        expandTabView.setValue(mTextArray, mViewArray);
//		expandTabView.setTitle(viewLeft.getShowText(), 0);
//		expandTabView.setTitle(viewMiddle.getShowText(), 1);
//		expandTabView.setTitle(viewRight.getShowText(), 2);

    }
    private void GetSearch_job(int pageNumber) {
        OkGo.<String>post(NetUrl.DNS + NetUrl.Joblist)
                .tag(this)
                .params("ProfessionName", searchBody)
                .params("pageIndex", pageNumber)
                .params("pageSize", 10)
                .params("WorkPlace", WorkPlace)
                .params("MinMoney",MinMoney)
                .params("Education",Education)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            int infoCode = jsonObject.getInt("status");
                            if (infoCode == 0) {
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                                total_pages = jsonObject1.getInt("pages");
                                JSONArray jsonArray = jsonObject1.getJSONArray("records");
                                System.out.println("返回的数据是" + jsonArray);
                                if (jsonArray.length() == 0) {

                                } else {
                                    jobInfoList = new ArrayList<>();
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                                        JobInfo jobInfo = new JobInfo();
                                        jobInfo.setLocation(jsonObject2.getString("workPlace"));
                                        jobInfo.setDate(jsonObject2.getString("startTime"));
//                                        String beginDate = jsonObject2.getString("createTime");
//                                        jobInfo.setDate(total_pagesTools.getStrTime(beginDate));
                                        jobInfo.setCompany(jsonObject2.getString("cname"));
                                        jobInfo.setId(jsonObject2.getString("id"));
                                        jobInfo.setJob(jsonObject2.getString("title"));
                                        jobInfo.setPrice(jsonObject2.getInt("minMoney") + "-" + jsonObject2.getInt("maxMoney"));
                                        jobInfoList.add(jobInfo);
                                    }
                                    jobAdapter.addData(jobInfoList);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                jobAdapter.clear();
                pageNumber = 1;
                GetSearch_job(pageNumber);
                swipeToLoadLayout.setRefreshing(false);
            }
        }, 100);
    }

    @Override
    public void onLoadMore() {
        if (pageNumber >= total_pages) {
            Tools.toast(SearchByproActivity.this, "已全部加载完成!");
            swipeToLoadLayout.setLoadingMore(false);
        } else {
            swipeToLoadLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    pageNumber++;
                    GetSearch_job(pageNumber);
                    swipeToLoadLayout.setLoadingMore(false);
                }
            }, 100);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            searchBody = searchEdit.getText().toString().trim();
            jobAdapter.clear();
            pageNumber = 1;
            GetSearch_job(pageNumber);
        }
        return false;
    }
    private void initListener() {

        viewLeft.setOnSelectListener(new ViewLeft.OnSelectListener() {

            @Override
            public void getValue(String distance, String showText) {
                System.out.println("点击了"+showText);
                onRefresh(viewLeft, showText);
                if (showText.equals("不限")){
                    MinMoney = "";
                }else{
                    MinMoney = showText.replaceAll("以上","");
                }
                jobAdapter.clear();
                pageNumber = 1;
                GetSearch_job(pageNumber);
            }
        });

        viewMiddle.setOnSelectListener(new ViewMiddle.OnSelectListener() {

            @Override
            public void getValue(String showText) {
                System.out.println("点击了"+showText);
                onRefresh(viewMiddle,showText);
                if (showText.equals("不限")){
                    WorkPlace = "";
                }else{
                    WorkPlace = showText;
                }
                jobAdapter.clear();
                pageNumber = 1;
                GetSearch_job(pageNumber);
            }
        });

        viewRight.setOnSelectListener(new ViewRight.OnSelectListener() {

            @Override
            public void getValue(String distance, String showText) {
                System.out.println("点击了"+showText);
                onRefresh(viewRight, showText);
                if (showText.equals("不限")){
                    Education = "";
                }else{
                    Education = showText;
                }
                jobAdapter.clear();
                pageNumber = 1;
                GetSearch_job(pageNumber);
            }
        });

    }
    private void onRefresh(View view, String showText) {

        expandTabView.onPressBack();
        int position = getPositon(view);
        if (position >= 0 && !expandTabView.getTitle(position).equals(showText)) {
            expandTabView.setTitle(showText, position);
        }
//        Toast.makeText(MainActivity.this, showText, Toast.LENGTH_SHORT).show();

    }

    private int getPositon(View tView) {
        for (int i = 0; i < mViewArray.size(); i++) {
            if (mViewArray.get(i) == tView) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void onBackPressed() {

        if (!expandTabView.onPressBack()) {
            finish();
        }

    }
    public void returns(View view){
        finish();
    }
}